package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.system.domain.Equipment;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Car;
import com.ruoyi.system.service.ICarService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 汽车Controller
 *
 * @author ruoyi
 * @date 2024-04-11
 */
@RestController
@RequestMapping("/system/car")
public class CarController extends BaseController
{
    @Autowired
    private ICarService carService;


    @Log(title = "汽车管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:car:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<Car> util = new ExcelUtil<>(Car.class);
        List<Car> carList = util.importExcel(file.getInputStream());
        String message = carService.importCars(carList, updateSupport);
        return AjaxResult.success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<Car> util = new ExcelUtil<Car>(Car.class);
        util.importTemplateExcel(response, "汽车数据");
    }

    @Anonymous
    @GetMapping("/frontList")
    public AjaxResult frontList()
    {
        return success(carService.selectCarList(new Car()));
    }

    @Anonymous
    @GetMapping("/charts")
    public List<Car> charts(Car car)
    {
        return carService.selectCarList(car);
    }

    /**
     * 查询汽车列表
     */
    @PreAuthorize("@ss.hasPermi('system:car:list')")
    @GetMapping("/list")
    public TableDataInfo list(Car car)
    {
        startPage();
        List<Car> list = carService.selectCarList(car);
        return getDataTable(list);
    }

    /**
     * 导出汽车列表
     */
    @PreAuthorize("@ss.hasPermi('system:car:export')")
    @Log(title = "汽车", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Car car)
    {
        List<Car> list = carService.selectCarList(car);
        ExcelUtil<Car> util = new ExcelUtil<Car>(Car.class);
        util.exportExcel(response, list, "汽车数据");
    }

    /**
     * 获取汽车详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:car:query')")
    @GetMapping(value = "/{carId}")
    public AjaxResult getInfo(@PathVariable("carId") Long carId)
    {
        return success(carService.selectCarByCarId(carId));
    }

    /**
     * 新增汽车
     */
    @PreAuthorize("@ss.hasPermi('system:car:add')")
    @Log(title = "汽车", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Car car)
    {
        return toAjax(carService.insertCar(car));
    }

    /**
     * 修改汽车
     */
    @PreAuthorize("@ss.hasPermi('system:car:edit')")
    @Log(title = "汽车", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Car car)
    {
        return toAjax(carService.updateCar(car));
    }

    /**
     * 删除汽车
     */
    @PreAuthorize("@ss.hasPermi('system:car:remove')")
    @Log(title = "汽车", businessType = BusinessType.DELETE)
    @DeleteMapping("/{carIds}")
    public AjaxResult remove(@PathVariable Long[] carIds)
    {
        return toAjax(carService.deleteCarByCarIds(carIds));
    }
}
